package it.traveldream.viewer;

import it.traveldream.customer.RichiesteAcquistiBean;
import it.traveldreamClient.beans.GestoreRichiesteBeanInterface;
import it.traveldreamClient.beans.PacchettiMgrBeanInterface;
import it.traveldreamClient.beans.UserMgr;
import it.traveldreamClient.beans.dto.AlloggioDTO;
import it.traveldreamClient.beans.dto.EscursioneDTO;
import it.traveldreamClient.beans.dto.PkgDTO;
import it.traveldreamClient.beans.dto.PkgPersonalDTO;
import it.traveldreamClient.beans.dto.VoloDTO;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.swing.text.StyledEditorKit.BoldAction;
 
@ManagedBean(name="showPkgMB")
@SessionScoped
public class ShowPacchettoManagedBean {
	@EJB 
	private UserMgr usr;
	@EJB
	private PacchettiMgrBeanInterface pkg;
	@EJB
	private GestoreRichiesteBeanInterface rich;
	
		private PkgDTO PacchettoDTO;

		private boolean el, acq, per, chan, psz;		


		public void setEl(boolean el) {
			this.el = el;
		}


		public void setAcq(boolean acq) {
			this.acq = acq;
		}


		public boolean isEl() {
			if(usr.getUrlRole().equals("user")) 
				el=false; 
			else 
				el=true; 
			return el;
		}


		public boolean isAcq() {
			if(usr.getUrlRole().equals("user")) 
				acq=true; 
			else 
				acq=false; 
			return acq;
		}


		public boolean isPer() {
			if(usr.getUrlRole().equals("user")) 
				per=true; 
			else 
				per=false; 
			return per;	
		}
		

		public void setPer(boolean per) {
			this.per = per;
		}

		public boolean isChan() {
			if(usr.getUrlRole().equals("user")) 
				chan=false; 
			else 
				chan=true; 
			return chan;
		}


		public void setChan(boolean chan) {
			this.chan = chan;
		}


		public ShowPacchettoManagedBean() {
			super();
		}

		

		public PkgDTO getPacchettoDTO() {
			return PacchettoDTO;
		}

		public void setPacchettoDTO(PkgDTO pacchettoDTO) {
			PacchettoDTO = pacchettoDTO;
		}

		public String acquista(){
			if(PacchettoDTO instanceof PkgPersonalDTO){
				PkgPersonalDTO pk=(PkgPersonalDTO)PacchettoDTO;
				int num=rich.getRichiesteAccettate(pk);
				if(num<pk.getMaxPartecipanti()){
					usr.acquista(PacchettoDTO.getIdPacc());
					rich.incrementaRichiesteAccettate(pk);
				}
			}else{
				usr.acquista(PacchettoDTO.getIdPacc());
			}
			return "/user/acquistoAvvenuto?faces-redirect=true";
		}

		public UserMgr getUsr() {
			return usr;
		}


		public void setUsr(UserMgr usr) {
			this.usr = usr;
		}


		public PacchettiMgrBeanInterface getPkg() {
			return pkg;
		}


		public void setPkg(PacchettiMgrBeanInterface pkg) {
			this.pkg = pkg;
		}


		public void setPsz(boolean psz) {
			this.psz = psz;
		}


		public String loadPkg(){
		       /* FacesContext facesContext = FacesContext.getCurrentInstance();  
		        ExternalContext externalContext = facesContext.getExternalContext();  
		  
		        Map<String, String> params = externalContext.getRequestParameterMap();*/  
		        //inPacchettoDTO = new PkgDTO();
		        //idTomodify=params.get("idPacchetto");
			
			if(usr.getMailActualUser().equals("ANONYMOUS"))
				return "requestResponsePage";
			
		    if(PacchettoDTO instanceof PkgPersonalDTO){
		    	PkgPersonalDTO pk=(PkgPersonalDTO)PacchettoDTO;
		    	int num=rich.getRichiesteAccettate(pk);
		    	if(num==pk.getMaxPartecipanti()){
		    		return "pacchettoAlCompleto";
		    	}else {
		    		return "/user/showPacchettoPersonal?faces-redirect=true";
		    	}
		    
		    }else{
		    	return "/share/showPacchetto?faces-redirect=true";
		    }
		    
		}


		public boolean isPsz() {
		System.out.println("oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo");
			if(((PkgPersonalDTO) PacchettoDTO).getEmailP().equals(usr.getMailActualUser()))
				psz=true;
			else
				psz=false;
			return psz;
		}


	
		public String eliminaPacchetto(){
			pkg.deletePacchetto(PacchettoDTO.getIdPacc());
	    	return "/employ/index?faces-redirect=true";

		}
}


